﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Frm_Repertoire_tele
    Public num As Integer = 0
    Public modif As Integer = 0

    Public Sub vider()

        txt_nom.Clear()
        txt_prenom.Clear()
        txt_adresse.Clear()
        txt_mail.Clear()
        txt_fix.Clear()
        txt_fax.Clear()
        txt_metier.Clear()
        txt_portabl.Clear()
        txt_remarq.Clear()
        txt_nom.Select()
        modif = 0
    End Sub

    Public Sub Afficher()
        Try
            List_tous.Items.Clear()
            Cmd = New SqlCommand("Select * From Repertoire", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Nom"), Dr("Prenom"), Dr("Metier"), Dr("Adresse"), Dr("Email"), Dr("Portable"), Dr("Remarque"), Dr("Id_Repertoire")})
                List_tous.Items.Add(Line)
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub btn_recher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_recher.Click
        Frm_Recherche_tele.Show()
    End Sub

    Private Sub Frm_Repertoire_tele_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Afficher()
        txt_nom.Select()
    End Sub

    Private Sub btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nouv.Click
        vider()
    End Sub

    Private Sub btn_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter.Click
        If (txt_nom.Text <> "" And txt_prenom.Text <> "") Then
            Dim var As Boolean = regEmail(txt_mail.Text)
            If txt_mail.Text = "" Then
                var = True
            End If
            If var = True Then
                If MsgBox("Vous Voulez Vraiment Ajouter  ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter ") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("insert into Repertoire (Nom,Prenom,Metier,Adresse,Email,Remarque,Portable,Fix,Fax)" & _
                        "values (@nom,@pren,@Metier,@adres,@email,@Remarque,@Portable,@Fix,@Fax)", Con)

                        Cmd.Parameters.Add("@nom", SqlDbType.VarChar).Value = txt_nom.Text
                        Cmd.Parameters.Add("@pren", SqlDbType.VarChar).Value = txt_prenom.Text
                        Cmd.Parameters.Add("@Metier", SqlDbType.VarChar).Value = txt_metier.Text
                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq.Text
                        Cmd.Parameters.Add("@adres", SqlDbType.VarChar).Value = txt_adresse.Text
                        Cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txt_mail.Text

                        Cmd.Parameters.Add("@Portable", SqlDbType.VarChar).Value = txt_portabl.Text
                        Cmd.Parameters.Add("@Fix", SqlDbType.VarChar).Value = txt_fix.Text
                        Cmd.Parameters.Add("@Fax", SqlDbType.VarChar).Value = txt_fax.Text

                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()

                        MsgBox("Ajouté Avec Succes", MsgBoxStyle.Information, "Réussi !")
                        vider()
                        Afficher()

                    Catch ex As Exception

                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If
            Else
                MsgBox("Entrez l'email Correct", MsgBoxStyle.Critical, "Erreur !")
            End If
        Else
            MsgBox("Entrez les Données", MsgBoxStyle.Critical, "Erreur !")
        End If
    End Sub

    Private Sub btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp.Click


        Dim n As Integer
        With List_tous
            If .SelectedIndices.Count > 0 Then
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    n = CInt(.Items(.SelectedIndices(i)).SubItems(7).Text)
                Next
                If MsgBox("Voulez Vous Vraiment Supprimer ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Supprission !") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("Delete From Repertoire Where Id_Repertoire =" & n, Con)
                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()
                        Afficher()
                        vider()
                        modif = 0
                        MsgBox("Suppresion effectué !", MsgBoxStyle.Information, "Terminer")
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If
            Else
                MsgBox("Vous n'avez selectionner aucun Ligne !", MsgBoxStyle.Critical, "Réssayer !")
            End If
        End With

    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        vider()
        Dim n As Integer
        With List_tous
            If .SelectedIndices.Count > 0 Then
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    n = CInt(.Items(.SelectedIndices(i)).SubItems(7).Text)
                    modif = CInt(.Items(.SelectedIndices(i)).SubItems(7).Text)
                Next
            End If
        End With

        Try
            Cmd = New SqlCommand("Select * From Repertoire where Id_Repertoire =" & n, Con)
            open()
            Dr = Cmd.ExecuteReader()
            Dr.Read()
            txt_nom.Text = Dr("Nom")
            txt_prenom.Text = Dr("Prenom")
            txt_metier.Text = Dr("Metier")
            txt_adresse.Text = Dr("Adresse")
            txt_mail.Text = Dr("Email")
            txt_portabl.Text = Dr("Portable")
            txt_fax.Text = Dr("Fax")
            txt_fix.Text = Dr("Fix")
            txt_remarq.Text = Dr("Remarque")
            Dr.Close()
            cnClose()
        Catch ex As Exception

            'MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_modif.Click
        If modif <> 0 Then

            If (txt_nom.Text <> "" And txt_prenom.Text <> "") Then
                If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("update Repertoire set Nom=@nom,Prenom=@pren,Metier=@Metier,Adresse=@adres,Email=@email,Remarque=@Remarque,Portable=@Portable,Fix=@Fix,Fax=@Fax" & _
                        " Where Id_Repertoire=" & modif, Con)


                        Cmd.Parameters.Add("@nom", SqlDbType.VarChar).Value = txt_nom.Text
                        Cmd.Parameters.Add("@pren", SqlDbType.VarChar).Value = txt_prenom.Text
                        Cmd.Parameters.Add("@Metier", SqlDbType.VarChar).Value = txt_metier.Text
                        Cmd.Parameters.Add("@Remarque", SqlDbType.Text).Value = txt_remarq.Text
                        Cmd.Parameters.Add("@adres", SqlDbType.VarChar).Value = txt_adresse.Text
                        Cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txt_mail.Text

                        Cmd.Parameters.Add("@Portable", SqlDbType.VarChar).Value = txt_portabl.Text
                        Cmd.Parameters.Add("@Fix", SqlDbType.VarChar).Value = txt_fix.Text
                        Cmd.Parameters.Add("@Fax", SqlDbType.VarChar).Value = txt_fax.Text

                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()

                        Afficher()
                        vider()
                        modif = 0
                        MsgBox("Modification a Réussi ", MsgBoxStyle.Information, "Réussi !")
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try


                End If
            Else
                MsgBox("Vous n'avez selectionner aucun Ligne !", MsgBoxStyle.Critical, "Réssayer !")
            End If
        End If
        modif = 0
    End Sub

End Class